﻿using UnityEngine;
using System.Collections;
using KeyBindings;
using PlayerClasses;

namespace EnemyClasses
{
	// Require these components when using this script
	[RequireComponent(typeof(Animator))]

	/// <summary>
	/// Enemy AI.
	/// </summary>
	public class EnemyAI : MonoBehaviour
	{
		public float animSpeed = 1.5f;				// a public setting for overall animator animation speed
		public float AttackArea = 1.5f;
		public float AttackCoolDown = 1.0f;

		private Enemy enemy;
		private Rigidbody rig;
		private Animator anim;							// a reference to the animator on the character
		private AnimatorStateInfo currentBaseState;			// a reference to the current state of the animator, used for base layer
		private AnimatorStateInfo layer2CurrentState;	// a reference to the current state of the animator, used for layer 2
		
		float time = 0;
		bool attack = false;
		bool death = false;
		public float speed;
		public float rotSpeed;
		private float h;
		private float v;
		
		void Start ()
		{
			// initialising reference variables
			anim = GetComponent<Animator> ();					  
			rig = GetComponent<Rigidbody> ();
			enemy = GetComponent<Enemy> ();

			//enemy = GameObject.Find("Enemy").transform;	
			if (anim.layerCount == 2) {
				anim.SetLayerWeight (1, 1);
			}
			
			h = 0;
			v = 0;

		}

		void OnDestroy ()
		{
			Debug.Log ("Enemy Destroy");
		}
		
		void Update ()
		{	
			//If not attacking the player and not dead.
			if(!CheckAttack() && !death)
			{
				MoveToPlayer();
			}
		}

		/// <summary>
		/// Withins the distance.
		/// </summary>
		/// <returns><c>true</c>, if distance was withined, <c>false</c> otherwise.</returns>
		/// <param name="playerPos">Player position.</param>
		private bool WithinDistance (Transform playerPos)
		{
			return (Vector3.Distance (transform.position, playerPos.position) <= AttackArea);
		}

		/// <summary>
		/// Checks to see if the enAttack the player.
		/// <returns><c>true</c>, if distance of the player is close enough, <c>false</c> if not close.</returns>
		/// </summary>
		private bool CheckAttack ()
		{
			//Make sure if the player is within the right distance
			if (WithinDistance (PlayerController.playerPos)) {
				//Cooldown is ready
				if (!attack) {
					Attack ();
				}
				return true;
			}
			v = 1;
			return false;
		}

		/// <summary>
		/// Attacks the player.
		/// </summary>
		private void Attack ()
		{
			attack = true;
			v = 0;
			Invoke ("CancelAttack", AttackCoolDown);
			PlayerController.damage(5);

		}

		/// <summary>
		/// Cancels attack.
		/// </summary>
		private void CancelAttack ()
		{
			attack = false;
		}

		/// <summary>
		/// Moves to the player.
		/// </summary>
		private void MoveToPlayer ()
		{
			//Insert Player move code
			transform.LookAt (PlayerController.playerPos);
			transform.Translate (Vector3.forward * ((speed * Time.deltaTime)/2.0f));
		}

		/// <summary>
		/// Enables ragdoll.
		/// </summary>
		void Kill ()
		{
			v = 0;
			anim.enabled = false;
			rig.isKinematic = false;
			death = true;
			time = 0;
		}
		
		void FixedUpdate ()
		{			
			anim.SetFloat ("Speed", v);							// set our animator's float parameter 'Speed' equal to the vertical input axis				
			anim.SetFloat ("Direction", h); 						// set our animator's float parameter 'Direction' equal to the horizontal input axis		
			anim.SetFloat ("Time", time);
			anim.SetBool ("Attack", attack);
			anim.SetBool ("Death", death);
			anim.speed = animSpeed;								// set the speed of our animator to the public variable 'animSpeed'
			currentBaseState = anim.GetCurrentAnimatorStateInfo (0);	// set our currentState variable to the current state of the Base Layer (0) of animation
			
			if (anim.layerCount == 2)		
				layer2CurrentState = anim.GetCurrentAnimatorStateInfo (1);	// set our layer2CurrentState variable to the current state of the second Layer (1) of animation
			
			if (!enemy.Alive) {
				if (death) {
					if (time >= 5) {
						Destroy (gameObject);
					}
				} else {
					Kill ();
				}
			}

			//TIME
			if (time < 10f) {
				time += Time.deltaTime;
			} else {
				time = 0;
			}

		}
	}
}